package com.exercise.widgets.viewpager2.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TableLayout.LayoutParams
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.exercise.widgets.R
import com.exercise.widgets.databinding.FragmentNestViewpagerBinding
import com.google.android.material.tabs.TabLayoutMediator

class NestViewPagerFragment : Fragment() {
    private var _binding: FragmentNestViewpagerBinding? = null
    private val binding get() = _binding!!

    private val mTitle: List<Pair<String, Fragment>> = listOf(
        "Child1" to NormalFragment.newInstance("Child1"),
        "Child2" to NormalFragment.newInstance("Child2"),
        "Child3" to NormalFragment.newInstance("Child3"),
    )

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentNestViewpagerBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return mTitle.size
            }

            override fun createFragment(position: Int): Fragment {
                return mTitle[position].second
            }
        }
        TabLayoutMediator(
            binding.tabLayout,
            binding.viewPager2
        ) { tab, position ->
            tab.text = mTitle[position].first
            tab.setIcon(R.drawable.ic_person_24)
        }.attach()

        val badge = binding.tabLayout.getTabAt(1)?.orCreateBadge
//        badge?.number = 100
//        badge?.setContentDescriptionNumberless("aaa")
//        badge?.setContentDescriptionForText("ccc")
        badge?.setContentDescriptionExceedsMaxBadgeNumberStringResource(R.string.app_name)
    }

    companion object {
        fun newInstance(): NestViewPagerFragment {
            return NestViewPagerFragment()
        }
    }
}